package api

import (
	"github.com/gin-gonic/gin"
	"vtrip/global"
	"vtrip/internal/service"
	"vtrip/pkg/app"
	"vtrip/pkg/errcode"
)

type Article struct{}

func NewArticle() Article {
	return Article{}
}

// All
// @Summary 获取所有文章
// @Produce json
// @Param token header string true "Token"
// @Param page body number false "页码"
// @Param page_size body number false "页大小"
// @Router /article/all [post]
// @Success 200 {object} []model.ArticlePart "成功"
// @Failure 400 {object} errcode.Error "请求错误"
// @Failure 500 {object} errcode.Error "内部错误"
func (a Article) All(c *gin.Context) {
	// 参数校验
	param := service.GetAllArticleRequest{}
	response := app.NewResponse(c)
	valid, errs := app.BindAndValid(c, &param)
	if !valid {
		global.Logger.Errorf("app.BindAndValid errs: %v", errs)
		errResponse := errcode.InvalidParams.WithDetails(errs.Errors()...)
		response.ToErrorResponse(errResponse)
		return
	}

	// 业务处理
	svc := service.New(c.Request.Context())
	articles, err := svc.GetAllArticle(&param)
	if err != nil {
		global.Logger.Errorf("svc.GetAllArticle err: %v", err)
		response.ToErrorResponse(errcode.GetAllArticleFail)
		return
	}

	response.ToSuccessResponse(gin.H{
		"articles": articles,
	})
}

// Get
// @Summary 获取某篇文章的详细信息
// @Produce json
// @Param token header string true "Token"
// @Param article_id body number true "文章 id"
// @Router /article/get [post]
// @Success 200 {object} model.ArticleDetail "成功"
// @Failure 400 {object} errcode.Error "请求错误"
// @Failure 500 {object} errcode.Error "内部错误"
func (a Article) Get(c *gin.Context) {
	// 参数校验
	param := service.GetArticleDetailRequest{}
	response := app.NewResponse(c)
	valid, errs := app.BindAndValid(c, &param)
	if !valid {
		global.Logger.Errorf("app.BindAndValid errs: %v", errs)
		errResponse := errcode.InvalidParams.WithDetails(errs.Errors()...)
		response.ToErrorResponse(errResponse)
		return
	}

	// 业务处理
	svc := service.New(c.Request.Context())
	userId := c.GetInt("user_id")
	article, err := svc.GetArticleDetail(userId, &param)
	if err != nil {
		global.Logger.Errorf("svc.GetArticleDetail err: %v", err)
		response.ToErrorResponse(errcode.GetAllArticleFail)
		return
	}

	response.ToSuccessResponse(gin.H{
		"article": article,
	})
}

// Publish
// @Summary 发表文章
// @Produce json
// @Param token header string true "Token"
// @Param title body string true "文章标题"
// @Param cover_url body string false "封面地址"
// @Param introduce body string true "文章简介"
// @Param content body string true "文章内容"
// @Router /article/publish [post]
// @Success 200 {object} nil "成功"
// @Failure 400 {object} errcode.Error "请求错误"
// @Failure 500 {object} errcode.Error "内部错误"
func (a Article) Publish(c *gin.Context) {
	// 参数校验
	param := service.PublishArticleRequest{}
	response := app.NewResponse(c)
	valid, errs := app.BindAndValid(c, &param)
	if !valid {
		global.Logger.Errorf("app.BindAndValid errs: %v", errs)
		errResponse := errcode.InvalidParams.WithDetails(errs.Errors()...)
		response.ToErrorResponse(errResponse)
		return
	}

	// 业务处理
	svc := service.New(c.Request.Context())
	userId := c.GetInt("user_id")
	err := svc.PublishArticle(userId, &param)
	if err != nil {
		global.Logger.Errorf("svc.PublishArticle err: %v", err)
		response.ToErrorResponse(errcode.PublishArticleFail)
		return
	}

	response.ToSuccessResponse(nil)
}

// Like
// @Summary 为文章点赞
// @Produce json
// @Param token header string true "Token"
// @Param article_id body number true "文章 id"
// @Router /article/like [post]
// @Success 200 {object} nil "成功"
// @Failure 400 {object} errcode.Error "请求错误"
// @Failure 500 {object} errcode.Error "内部错误"
func (a Article) Like(c *gin.Context) {
	// 参数校验
	param := service.LikeArticleRequest{}
	response := app.NewResponse(c)
	valid, errs := app.BindAndValid(c, &param)
	if !valid {
		global.Logger.Errorf("app.BindAndValid errs: %v", errs)
		errResponse := errcode.InvalidParams.WithDetails(errs.Errors()...)
		response.ToErrorResponse(errResponse)
		return
	}

	// 业务处理
	svc := service.New(c.Request.Context())
	userId := c.GetInt("user_id")
	err := svc.LikeArticle(userId, &param)
	if err != nil {
		global.Logger.Errorf("svc.LikeArticle err: %v", err)
		response.ToErrorResponse(errcode.PublishArticleFail)
		return
	}

	response.ToSuccessResponse(nil)
}

// Dislike
// @Summary 取消点赞
// @Produce json
// @Param token header string true "Token"
// @Param article_id body number true "文章 id"
// @Router /article/dislike [post]
// @Success 200 {object} nil "成功"
// @Failure 400 {object} errcode.Error "请求错误"
// @Failure 500 {object} errcode.Error "内部错误"
func (a Article) Dislike(c *gin.Context) {
	// 参数校验
	param := service.DislikeArticleRequest{}
	response := app.NewResponse(c)
	valid, errs := app.BindAndValid(c, &param)
	if !valid {
		global.Logger.Errorf("app.BindAndValid errs: %v", errs)
		errResponse := errcode.InvalidParams.WithDetails(errs.Errors()...)
		response.ToErrorResponse(errResponse)
		return
	}

	// 业务处理
	svc := service.New(c.Request.Context())
	userId := c.GetInt("user_id")
	err := svc.DisLikeArticle(userId, &param)
	if err != nil {
		global.Logger.Errorf("svc.DisLikeArticle err: %v", err)
		response.ToErrorResponse(errcode.PublishArticleFail)
		return
	}

	response.ToSuccessResponse(nil)
}
